﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iBuproModel;
using iBuproModel.Repositories;
using iBuproModel.Repositories.Classes;
using IBupro.Models;
using IBupro.Controllers.Classes;
using iBuproModel.Repositories.Interfaces;

namespace IBupro.Controllers
{

    /// <summary>
    /// Handles student plans business logic
    /// </summary>
    public class StudentPlanController : CustomController
    {
        IStudentPlanRepository repository;
        public StudentPlanController()
        {
            this.repository = repositoryService.StudentPlanRepositoryCreateInstance();
        }

        public  StudentPlanController(IStudentPlanRepository repository)
        {
            this.repository = repository;
        }

        [Authorize(Roles="Admin")]
        public ActionResult Index()
        {
            return View(repository.GetAll());
        }

        //
        // GET: /StudentPlan/Details/5
        [Authorize(Roles = "Admin")]
        public ActionResult Details(int id)
        {
            return View(repository.First(p => p.Id == id));
        }

        //
        // GET: /StudentPlan/Create
        [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            return View(new StudentPlanModel());
        } 

        //
        // POST: /StudentPlan/Create
        [Authorize(Roles = "Admin")]
        [HttpPost]
        public ActionResult Create(StudentPlanModel studentplan)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    StudentPlan newPlan = new StudentPlan();
                    newPlan.Name = studentplan.Name;
                    newPlan.Shortcut = studentplan.Shortcut;
                    
                    if (studentplan.SelectedSubjects != null)
                        repository.UpdateSubjectList(newPlan, studentplan.SelectedSubjects);

                    if (studentplan.SelectedFields != null)
                        repository.UpdateStudentFieldList(newPlan, studentplan.SelectedFields);

                    repository.Add(newPlan);
                    repository.SaveChanges();
                }
                else
                    return View(studentplan);
                return RedirectToAction("Index");
            }
            catch
            {
                return View(studentplan);
            }
        }
        
        //
        // GET: /StudentPlan/Edit/5
        [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id)
        {
            StudentPlan storedPlan = repository.First(p => p.Id == id);
            StudentPlanModel model = new StudentPlanModel();

            model.Name = storedPlan.Name;
            model.Shortcut = storedPlan.Shortcut;

            List<string> selected_subjects = new List<string>();

            foreach (Subject subject in storedPlan.Subjects)
            {
                selected_subjects.Add(subject.Id.ToString());
            }

            List<string> selected_fields = new List<string>();

            foreach (StudentField field in storedPlan.StudentFields)
            {
                selected_fields.Add(field.Id.ToString());
            }

            ViewData["selected_subjects"] = selected_subjects.ToArray();
            ViewData["selected_fields"] = selected_fields.ToArray();

            return View(model);
        }

        //
        // POST: /StudentPlan/Edit/5
        [Authorize(Roles = "Admin")]
        [HttpPost]
        public ActionResult Edit(int id,StudentPlanModel studentplan)
        {
            try
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        StudentPlan newPlan = repository.GetSingle(p => p.Id == id);
                        newPlan.Name = studentplan.Name;
                        newPlan.Shortcut = studentplan.Shortcut;

                        newPlan.Subjects.Clear();

                        if (studentplan.SelectedSubjects != null)
                            repository.UpdateSubjectList(newPlan, studentplan.SelectedSubjects);

                        if (studentplan.SelectedFields != null)
                            repository.UpdateStudentFieldList(newPlan, studentplan.SelectedFields);


                        repository.SaveChanges();
                    }
                    else
                        return View(studentplan);
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // POST: /StudentPlan/Delete/5
        [Authorize(Roles = "Admin")]
        public ActionResult Delete(int id)
        {
            try
            {

                repository.Delete(p => p.Id == id);
                repository.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
